#!/usr/bin/node
//const EventEmitter = require('events').EventEmitter;

function Dog(name, energy) {
  //let _listeners = {
  //'event1':[listener1, listener2,...],
  //'event2':[]
  //} 
  
  let _listeners = {};

  //var _name = name,
  // _energy = energy;
  this.name = name;
  this.energy = energy;

  //EventEmitter.call(this);

  var timer = setInterval(() => {
    if(energy > 0) {
      emit('bark');
      this.energy--;
    }

    if(this.energy < 0) {
      clearInterval(timer);
    }


  }, 1000);

  //this.name = () => _name;
  //this.energy = () => _energy;

  this.on = (evt, fn) => {
    if(typeof(_listeners[evt]) === 'undefined') {
      _listeners[evt] = [];
    }
    _listeners[evt].push(fn);
  };
  function emit(evt, arg) {

    if(typeof(_listeners[evt]) === 'undefined') {
      console.error('error:%s is not defined', evt);
      process.exit(1);
    }

    _listeners[evt].forEach((fn) => {
      fn.call(this, arg);
    });
  }

}

//Dog.prototype = EventEmitter.prototype;
module.exports = Dog;
